﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerPlanDePago: Broker
    {
        #region constructor

        public BrokerPlanDePago()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {
            PlanDePago planDePagoAux = (PlanDePago)o;
            this.conn.connect();
            string sql = "insert into PlanesDePago(Nombre, CantidadDias, Descuento) values('" 
                + planDePagoAux.Nombre + "', " + planDePagoAux.CantDias + ", " + planDePagoAux.Descuento + ")";
            this.conn.execute(sql);            
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            PlanDePago planDePagoAux = (PlanDePago)o;
            string sql = "update PlanesDePago set Nombre = '" + planDePagoAux.Nombre
                + "', CantidadDias = " + planDePagoAux.CantDias
                + ", Descuento = " + planDePagoAux.Descuento
                + " where IdPlan = " + planDePagoAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            PlanDePago planDePagoAux = (PlanDePago)o;                        
            string sql = "delete * from planesDePago where IdPlan = " + planDePagoAux.OID; 
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            PlanDePago planDePagoAux = (PlanDePago)o;
            this.conn.connect();
            string sql = "Select * from planesDePago where IdPlan = " + planDePagoAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {                
                planDePagoAux.Nombre = this.conn.getDataString("Nombre").ToString();
                planDePagoAux.CantDias = int.Parse(this.conn.getDataString("CantidadDias").ToString());
                planDePagoAux.Descuento = int.Parse(this.conn.getDataString("Descuento").ToString());
            }
            this.conn.disconnect();
        }

        public override List<Object> bringList(Object o)
        {
            string sql = "Select * from planesDepago";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                PlanDePago planDePagoAux = new PlanDePago();
                planDePagoAux.Nombre = this.conn.getDataString("Nombre").ToString();
                planDePagoAux.CantDias = int.Parse(this.conn.getDataString("CantidadDias").ToString());
                planDePagoAux.Descuento = int.Parse(this.conn.getDataString("Descuento").ToString());
                planDePagoAux.OID = int.Parse(this.conn.getDataString("IdPlan").ToString());
                list.Add(planDePagoAux);
            }
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            try
            {
                this.conn.connect();
                string sql = "Select max(idPlan) as idPlan from PlanesDePago";
                this.conn.bringData(sql);
                while (this.conn.nextRecord())
                {
                    id = int.Parse(this.conn.getDataString("idPlan").ToString());
                }
                this.conn.disconnect();
            }
            catch
            {
                id = 0;
            }
            return id;
        }

        #endregion
    }
}
